import React, { useEffect } from 'react';
import { BackHandler, Text, ToastAndroid } from 'react-native';
import 'react-native-gesture-handler';
import SplashScreen from 'react-native-splash-screen';
import { Provider } from 'react-redux';
import codePush from 'react-native-code-push';
// import { useNavigation } from 'react-navigation';
import AppWithNavigationState from './route/StartStack';
import configureStore from './redux/store/store';
import { PersistGate } from "redux-persist/integration/react";
import Login from './pages/Mine/Login/Login'

let time = 0;
// const navigation =  useNavigation();
const DEPLOYMENT_KEY = Platform.OS === 'ios' ? 'iOS的DeploymentKey' : 'tnxUwyYeDvnmOH-tLhVSz7j329dq6mInY1i7o';

function App(props) {
  // 启动页
  useEffect(() => {
    setTimeout(() => {
      SplashScreen.hide();
    }, 1000)
  })

  // 检查更新不能重启
  useEffect(() => {
    codePush.disallowRestart();//禁止重启
    // this.syncImmediate(); //开始检查更新 

    codePush.sync({
      //安装模式
      //ON_NEXT_RESUME 下次恢复到前台时
      //ON_NEXT_RESTART 下一次重启时
      //IMMEDIATE 马上更新
      mandatoryInstallMode: codePush.InstallMode.IMMEDIATE,
      deploymentKey: DEPLOYMENT_KEY,
      //对话框
      updateDialog: {
        //是否显示更新描述
        appendReleaseDescription: true,
        //更新描述的前缀。 默认为"Description"
        descriptionPrefix: "更新内容：",
        //强制更新按钮文字，默认为continue
        mandatoryContinueButtonLabel: "立即更新",
        //强制更新时的信息. 默认为"An update is available that must be installed."
        mandatoryUpdateMessage: "必须更新后才能使用",
        //非强制更新时，按钮文字,默认为"ignore"
        optionalIgnoreButtonLabel: '稍后',
        //非强制更新时，确认按钮文字. 默认为"Install"
        optionalInstallButtonLabel: '后台更新',
        //非强制更新时，检查到更新的消息文本
        optionalUpdateMessage: '有新版本了，是否更新？',
        //Alert窗口的标题
        title: '更新提示'
      }
    })
  })

  // 更新完毕重启
  useEffect(() => {
    codePush.allowRestart();
  }, [])

  // 返回键
  BackHandler.addEventListener('hardwareBackPress', () => {
    if (Date.now() - time > 2000) {
      ToastAndroid.show('是否退出应用', 100);
      time = Date.now();
      return true;
    } else {
      BackHandler.exitApp();
    }
    return true
  });

  const { store, persistor } = configureStore();
  return (
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <AppWithNavigationState />
      </PersistGate>
    </Provider>
  );
}

export default App;
